package com.caiyigo.baihua.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.sageras.util.ExcelUtil.ExcelLog;
import org.sageras.util.ExcelUtil.ExcelLogs;
import org.sageras.util.ExcelUtil.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.caiyigo.baihua.dto.DataGrid;
import com.caiyigo.baihua.dto.Pager;
import com.caiyigo.baihua.dto.Result;
import com.caiyigo.baihua.dto.ZhuanyeDto;
import com.caiyigo.baihua.model.Zhuanye;
import com.caiyigo.baihua.service.ZhuanyeService;
import com.github.miemiedev.mybatis.paginator.domain.Order;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;

/**
 * @author Administrator
 * 
 */
@Controller
@RequestMapping(value = "/business/zhuanye")
public class ZhuanyeController {
	@Autowired
	ZhuanyeService zhuanyeService;

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public @ResponseBody DataGrid<ZhuanyeDto> list(ZhuanyeDto dto, Pager page) {
		List<Order> orders = new ArrayList<Order>();
		orders.add(Order.create(page.getSort(), page.getOrder()));
		if (StringUtils.isNotEmpty(page.getQ())) {
			dto.setZyMc(page.getQ());
		}
		PageList<ZhuanyeDto> pageList = (PageList<ZhuanyeDto>) zhuanyeService
				.getList(dto, new PageBounds(page.getPage(), page.getRows(),
						orders, true));
		return new DataGrid<ZhuanyeDto>(
				pageList.getPaginator().getTotalCount(), pageList
						.getPaginator().getPage(), pageList);

	}

	@RequestMapping(value = "/select", method = RequestMethod.POST)
	public @ResponseBody DataGrid<ZhuanyeDto> select(ZhuanyeDto dto,
			DataGrid<ZhuanyeDto> dataGrid) {
		List<Order> orders = new ArrayList<Order>();
		orders.add(Order.create(dataGrid.getSort(), dataGrid.getOrder()));
		PageList<ZhuanyeDto> pageList = (PageList<ZhuanyeDto>) zhuanyeService
				.getList(dto, new PageBounds(1, 11, orders, true));
		dataGrid.setRows(pageList);
		return dataGrid;
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public @ResponseBody Result delete(
			@RequestParam(value = "zydh", required = true) String zydh) {

		return new Result(zhuanyeService.deleteByPrimaryKey(zydh) > 0);

	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public @ResponseBody Result add(@ModelAttribute("dto") ZhuanyeDto dto) {

		return new Result(zhuanyeService.insertSelective(dto) > 0);

	}

	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	public @ResponseBody Result edit(@ModelAttribute("user") ZhuanyeDto dto) {

		return new Result(zhuanyeService.updateByPrimaryKeySelective(dto) > 0);

	}

	@RequestMapping(value = "/export", method = RequestMethod.GET)
	public void export(Zhuanye zhuanye, Pager page, HttpServletResponse response) {
		response.setCharacterEncoding("UTF-8");
		String fileName = "baihua-zhuanye-" + System.currentTimeMillis()
				+ ".xls";
		List<Zhuanye> zhuanyepageList = zhuanyeService.getList(zhuanye,
				new PageBounds());
		response.setHeader("Content-Disposition", "inline; filename="
				+ fileName);
		// 确保发送的当前文本格式
		response.setContentType("application/vnd.ms-excel");
		try {
			ExcelUtil.exportExcel("百花教育开考专业表", zhuanyepageList,
					response.getOutputStream(), Zhuanye.class);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
	public @ResponseBody Result importExcel(
			@RequestParam(value = "file", required = true) MultipartFile file) {

		Map<String, Zhuanye> zs = zhuanyeService.getAll();
		ExcelLogs logs = new ExcelLogs();
		Collection<Zhuanye> list = null;

		try {
			list = ExcelUtil.importExcel(Zhuanye.class, file.getInputStream(),
					logs, 2);
		} catch (IOException e) {
			return new Result(false, "导入失败");
		}
		List<OtherResult> message = new ArrayList<OtherResult>();
		for (ExcelLog excelLog : logs.getErrorLogList()) {
			message.add(new OtherResult(String.valueOf(excelLog.getRowNum()),
					excelLog.getLog()));
			System.out.println(excelLog.getSheetIndex() + ":"
					+ excelLog.getRowNum() + "：" + excelLog.getLog());
		}
		if (message.size() == 0) {
			List<Zhuanye> flagList = new ArrayList<Zhuanye>();
			for (Zhuanye z : list) {
				if (null == zs.get(z.getZyDh())) {
					flagList.add(z);
					continue;
				}
				if (!zs.get(z.getZyDh()).equals(z)) {

					flagList.add(z);
				}
			}
			if (flagList.size() > 0) {
				zhuanyeService.batchUpdate(flagList);
			}
			return new Result(true);
		} else {
			return new Result(false, message);
		}

	}

}
